package com.my12306.train.controller;

import com.my12306.common.model.vo.PageVo;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.response.ResponseData;
import com.my12306.train.model.entity.TicketEntity;
import com.my12306.train.model.vo.TicketVo;
import com.my12306.train.model.vo.TrainNumberVo;
import com.my12306.train.service.TicketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 售票表
 *
 * @author ËÕÄÁÏ¦
 * @email
 * @date 2020-07-24 20:58:07
 */
@RestController
@RequestMapping("/ticket")
public class TicketController {
    @Autowired
    private TicketService ticketService;

    /**
     * 根据起始站和终点站检索
     * @return
     */
    @GetMapping("/query/param.html")
    public ResponseData queryTrainNumberParameter(){
       return  this.ticketService.queryTrainNumberParameter();
    }

    @PostMapping("/query/train.html")
    public ResponseData queryTrainNumber(@RequestBody TrainNumberVo trainNumberVo){
        if (trainNumberVo!=null) {
            return this.ticketService.queryTrainNumber(trainNumberVo);
        }else{
            return ResponseData.ok();
        }
    }
    @PostMapping("/query/ticket.html")
    public ResponseData queryTicket(@RequestBody @Validated TicketVo ticketVo){
        if (ticketVo!=null){
         return    this.ticketService.queryTicket(ticketVo);
        }
        return ResponseData.ok();
    }
    /**
     * 列表
     */
    @PostMapping("/list")
    public ResponseData list(@RequestBody PageVo pageVo) {
        PageUtils page = ticketService.queryTicketPage(pageVo);
        return ResponseData.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{ticketId}")
    public ResponseData info(@PathVariable("ticketId") Long ticketId) {
        TicketEntity ticket = ticketService.getById(ticketId);

        return ResponseData.ok().put("ticket", ticket);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResponseData save(@RequestBody TicketEntity ticket) {
        ticketService.save(ticket);

        return ResponseData.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResponseData update(@RequestBody TicketEntity ticket) {
        ticketService.updateById(ticket);

        return ResponseData.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public ResponseData delete(@RequestBody List<Long> ticketIds) {
        System.out.println(ticketIds);
        ticketService.deleteTicketBatchByIds(ticketIds);
        return ResponseData.ok();
    }

}
